package com.bosssoft.cloud.wechat.common.controller;

import com.bosssoft.cloud.wechat.common.bean.einvoice.CkParam;
import com.bosssoft.cloud.wechat.common.bean.einvoice.EinvoiceVO;
import com.bosssoft.cloud.wechat.common.bean.email.SendEipsEmailParam;
import com.bosssoft.cloud.wechat.common.infra.handler.EmailHandler;
import com.bosssoft.cloud.wechat.common.service.EInvoiceService;
import com.bosssoft.cloud.wechat.infrastructure.annotation.RequestParams;
import com.bosssoft.cloud.wechat.infrastructure.annotation.VerifyCode;
import com.bosssoft.cloud.wechat.infrastructure.bean.response.R;
import com.bosssoft.cloud.wechat.infrastructure.enums.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 查验票据
 *
 * @author wcy create on 2021/5/11 18:21 下午
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/einvoice")
@Api(value = "电子票查验api")
public class EinvoiceController {

    @Autowired
    private EInvoiceService eInvoiceService;

    @Resource
    private EmailHandler emailHandler;


    /**
     * 票号查验需要交款人名称
     * <p>
     *
     * @param param 查验参数,其中三要素是必要的
     * @return 电子票据信息
     */
    @ApiOperation("查验票据")
    @GetMapping(value = "check", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @VerifyCode(required = false)
    public R<?> check(@RequestParams CkParam param) {
        EinvoiceVO vo = eInvoiceService.check(param);
        return new R<>(ResultEnum.SUCCESS, vo);
    }

    /**
     * 发送邮箱
     *
     * @param param 发送的信息
     * @return 发送状态
     */
    @ApiOperation("发送票据到邮箱")
    @PostMapping("send_email")
    @VerifyCode
    public R<?> sendEmail(@RequestBody SendEipsEmailParam param) {
        emailHandler.send(param);
        return new R<>(ResultEnum.SEND_SUCCESS);
    }

    /**
     * 解析二维码中字符串信息
     * 注：
     * 微信公众号api识别二维码中的字符串返回给后端，此方法将该字符串中的信息解析并返回给前端
     * （交款人+电子票据代码+电子票据号码+校验码。。。）
     * eg:
     * CZ-EI-50,1.1.0,50060220,0000002655,071e3a,20210406,42.00
     *
     * @param qrcodeinfo 字符串
     * @return 识别出的代码号码等票据信息
     */
    @ApiOperation("解析二维码")
    @GetMapping("qrInquire")
    public R<?> qrInquire(String qrcodeinfo) {
        EinvoiceVO res = eInvoiceService.getInfoFromQrcode(qrcodeinfo);
        return new R<>(ResultEnum.PARSING_SUCCESS, res);
    }
}
